package com.windows;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.mysqld.Mysqld;
import com.style.StyleFont;
import com.tools.EasyCode;

import com.tools.Tools;

public class LoginView {

	//登录界面
	//定义一个界面
	JFrame jFrame=new JFrame();
	
	final int WIDTH=450;
	final int HEIGHT=275;
	
	public LoginView() {
		init();	
		jFrame.setResizable(false);//窗口是否可变
		jFrame.setVisible(true);//窗口是否可见
		jFrame.setDefaultCloseOperation(jFrame.DISPOSE_ON_CLOSE);//设置默认关闭方式
		jFrame.validate();//让组件生效
		
	
	}
	
	void init() {
		//设置窗口在屏幕中间  并设置大小
		Tools.setWindowPos(WIDTH, HEIGHT, jFrame);
		EasyCode.setTileIcon(jFrame);//设置标题和图标
		
		jFrame.setLayout(null);//设置为恐怖剧
		
		
		ImageIcon img=new ImageIcon(Tools.getPath()+"img\\"+"LoginBack.jpg");//加载图片
        JLabel bgimg = new JLabel(img);//定义一个标签用于存放图片
        bgimg.setBounds(0,0,WIDTH,HEIGHT);//设置标签大小
		
		
		
		
		
		//设置背景图片
		 //jframe.setLayout(new GridLayout());
	
        
        JPanel jPanel=new JPanel();//定义一个盘子装标题
        jPanel.setLayout(new FlowLayout(FlowLayout.CENTER,0,HEIGHT/3/2-36));
        jPanel.setOpaque(false);
        jPanel.setBounds(0, 0, WIDTH, HEIGHT/3);
        
        JLabel jLabel=new JLabel("学生信息管理系统");
        JLabel jLabel2=new JLabel("冯辉、丘志斌、马硕");
        jPanel.add(jLabel);
        jPanel.add(jLabel2);
        jLabel.setFont(StyleFont.titleFont);
        jLabel.setForeground(Color.GREEN);
        jLabel2.setFont(StyleFont.titleFont);
        jLabel2.setForeground(Color.BLACK);
        
        JPanel jPanel2=new JPanel();//存放账号和密码
        jPanel2.setLayout(new FlowLayout(FlowLayout.CENTER));//设置居中对齐
        jPanel2.setBounds(0, HEIGHT/3, WIDTH, HEIGHT/3-15);
        jPanel2.setOpaque(false);
       // jPanel2.setBackground(new Color(235, 229, 209));
        Box boxH;
        Box boxOne,boxTwo;
        boxH=Box.createHorizontalBox();
        boxOne=Box.createVerticalBox();
        boxTwo=Box.createVerticalBox();
        
       
        //______________________________________________________
      
        JLabel bgimg1 = new JLabel("账号");
    

    
        JTextField jTextField=new JTextField(14);
  
        
    
        JLabel bgimg2 = new JLabel("密码");

        
        
        JPasswordField jTextField2=new JPasswordField(14);
  
      
        
        
        
        //定义盘子3装  登录按钮 和 找回密码
        
        JPanel jPanel3=new JPanel();
        jPanel3.setLayout(new FlowLayout(FlowLayout.CENTER));
        jPanel3.setBounds(0, HEIGHT/3*2-15, WIDTH, HEIGHT/3+10);
       // jPanel3.setBackground(Color.PINK);
        jPanel3.setOpaque(false);
        
        //定义两个单选按钮 和安全登录放在一个盒子里面
        jPanel3.add(Box.createHorizontalStrut(130));
        JRadioButton jr1=new JRadioButton("管理员"); 
        JRadioButton jr2=new JRadioButton("老师"); 
        JRadioButton jr3=new JRadioButton("学生"); 
        ButtonGroup  bg1=new ButtonGroup(); 
        //设置成透明
        jr2.setOpaque(false);
        jr1.setOpaque(false);
        jr3.setOpaque(false);
        jr1.setSelected(true);//默认选择管理员
        
        bg1.add(jr2);
        bg1.add(jr1);
        bg1.add(jr3);
        //向前面添加 空心墙实现对齐
        jPanel3.add(jr1);
        jPanel3.add(jr2);
        jPanel3.add(jr3);
     
        for(int i=0;i<20;i++) {
        	   jPanel3.add(Box.createHorizontalStrut(1));
        }
     
 
        
        JButton jButton=new JButton("安全登录");
        jPanel3.add(jButton);
        jButton.setPreferredSize(new Dimension(175,27));
        jButton.setBackground(new Color(   91,192,222));
        jButton.setForeground(new Color(  228,243,248));
        jButton.setFont(StyleFont.loginFont);
        
        
        
        
        boxOne.add(bgimg1);
        boxOne.add(boxH.createVerticalStrut(10));//设置组件之间间隔
        boxOne.add(bgimg2);
        boxTwo.add(jTextField);
        boxTwo.add(boxH.createVerticalStrut(10));//设置组件之间间隔
        boxTwo.add(jTextField2);
        
        boxH.add(boxOne);
        boxH.add(boxH.createHorizontalStrut(10));//设置组件之间间隔
        boxH.add(boxTwo);
        jPanel2.add(boxH);
        
        
        //______________________________________________________
  
        



    
        //向前面添加 空心墙实现对齐
        
        
        
        
        
        
        
        
        
        
        
        
        
       jFrame.add(jPanel3);
       jFrame.add(jPanel2);
       jFrame.add(jPanel);
       jFrame.add(bgimg);
       
       
       jButton.addActionListener(new ActionListener() {
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(jr1.isSelected()) {
				
				//管理员select * from users_man where account='root' and password='root'
				String sqlString="select * from users_man where account=? and password=?";
				String data[]= {jTextField.getText(),Tools.getPassword(jTextField2)};
				ResultSet rs = Mysqld.QueryData(sqlString, data);
				try {
					int i=0;
					while(rs.next()) {
						i++;
					}
					
					
				
					
					if(i==1) {
						
						//登录
						ManageWindows manageWindows=new ManageWindows(jTextField.getText());
						jFrame.dispose();
						
					}else{
						Tools.messageWindows("账号密码错误");
					}
					
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
			}
			if(jr2.isSelected()) {
				
				//管理员
				String sqlString="select * from teacher where account=? and password=?";
				String data[]= {jTextField.getText(),Tools.getPassword(jTextField2)};
				ResultSet rs = Mysqld.QueryData(sqlString, data);
				try {
					int i=0;
					while(rs.next()) {
						i++;
					}
					
					
					
					
					if(i==1) {
						
						TeacherManWindows aManWindows=new TeacherManWindows(jTextField.getText());
						jFrame.dispose();
						
					}else{
						Tools.messageWindows("账号密码错误");
					}
					
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
			}
			
		
			if(jr3.isSelected()) {
				//员功
				//管理员
				String sqlString="select * from student where account=? and password=?";
				String data[]= {jTextField.getText(),Tools.getPassword(jTextField2)};
				ResultSet rs = Mysqld.QueryData(sqlString, data);
				try {
					int i=0;
					while(rs.next()) {
						i++;
					}
					if(i==0) {
						Tools.messageWindows("请检查账号密码是否正确?");
					}else {
						StudentWindows manageWindows=new StudentWindows(jTextField.getText());
						jFrame.dispose();
					}
					
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		
		
			}
			
			
			
			
			
			
		}
	});
   		
     //  jr3.setSelected(true);
      // jTextField.setText("1");
      // jTextField2.setText("1");
       
      // jButton.doClick();
  
   		
   		
        
	
		
	}
}
